package codewars.arrays;

import java.util.Arrays;
import java.util.List;

/**
 * Ones and Zeros
 * CodeWars: https://www.codewars.com/kata/ones-and-zeros/train/java
 * Topics: fundamentals, arrays
 *
 * 给一个列表，里面全是0，1。转换为十进制数字
 *
 * Created by yzy on 2019-05-07 15:02
 */
public class OnesAndZeros {

    public static void main(String[] args) {
        System.out.println(convertBinaryArrayToInt_lambda(Arrays.asList(1,0,0,1)));
    }

    public static int convertBinaryArrayToInt_lambda(List<Integer> binary) {
        return binary.stream().reduce((x,y) -> (x*2 + y)).get();
    }

    public static int convertBinaryArrayToInt(List<Integer> binary) {
        int bin = 1;
        int res = 0;
        for(int i=binary.size()-1; i>=0; i--){
            res = res + bin * binary.get(i);
            bin *=2;
        }
        return res;
    }
}
